var t, a = function(t) {
    return t && t.__esModule ? t : {
      default: t
    };
}(require("../../../../utils/toot.js")),
  o = getApp(),
  e = 0;

Page({
  data: {
    loadingBoo: !0,
    moreBoo: !0,
    oppositeList: [],
    imgUrl: "",
    imgUrlArr: [],
    upFlag: !0,
    upText: "问题查看",
    scrollTop: 2e3,
    currentResult: 0,
    msgIdArr: [],
    seqIdArr: [],
    doctorId:1,
    patientId:2,
    contactList:{
      doctorBaseInfoVO:{
        userIcon:'/assets/images/796.png',
        doctorName:'小红',
        jobType:1
      },
      dpMedOrderVo:{
        msgTxt:'嘎嘎',
        msgPicSmall: ['/assets/images/796.png', '/assets/images/796.png', '/assets/images/796.png',]
      }
    },
    upText:'2018-10-30',
    oppositeList:[
      {
        from:1,
        parting:1,
        ctime:'2018-10-30',
        msgType:'SINGLE_VIDEO',
        playFlag:'',
        duration:'回家',
        msg: '/assets/images/796.png',
        bigImg: '在外面'
      },
      {
        from: 2,
        parting: 2,
        ctime: '2018-10-30',
        msgType: 'SINGLE_IMAGE',
        playFlag: 10,
        duration: 20,
        msg: '/assets/images/796.png',
        bigImg: '在外面'
      },
      {
        from: 1,
        parting: 1,
        ctime: '2018-10-30',
        msgType: 'SINGLE_TEXT',
        playFlag: 10,
        duration: 20,
        msg: '感冒怎么治',
        bigImg: '在外面'
      },
      {
        from: 2,
        parting: 2,
        ctime: '2018-10-30',
        msgType: 'SINGLE_VIDEO',
        playFlag: 10,
        duration: 20,
        msg: '/assets/images/796.png',
        bigImg: '在外面'
      }
    ]
  },
  toPlay: function(t) {
    if (s) {
      var a = this;
      console.log(t), t.currentTarget.dataset.index == this.data.playIndex || !this.data.playIndex && 0 != this.data.playIndex || a.data.oppositeList[a.data.playIndex] && (a.data.oppositeList[a.data.playIndex].playFlag = !1,
          a.setData({
            oppositeList: a.data.oppositeList
          })), this.data.playIndex = t.currentTarget.dataset.index, s.src = t.currentTarget.dataset.src,
        this.data.oppositeList[this.data.playIndex].playFlag ? (s.stop(), a.data.oppositeList[a.data.playIndex].playFlag = !1,
          a.setData({
            oppositeList: a.data.oppositeList
          })) : s.play();
    } else wx.showModal({
      content: "您当前微信版本过低，暂不支持语音功能，请更新微信版本后重试",
      showCancel: !1,
      confirmColor: "#3b4ca9",
      confirmText: "我知道了"
    });
  },
  packUp: function() {
    var t = this.data.upFlag,
      a = this.data.upText;
    a = "问题查看" == a ? "收起" : "问题查看", this.setData({
      upFlag: !t,
      upText: a
    });
  },
  previewImage: function(t) {
    var a = this.data.contactList.dpMedOrderVo.msgPicBig,
      e = t.target.dataset.imgindex;
    o.previewImage(t, a, e);
  },
  bigImage: function(t) {
    console.log(t);
    var a = this;
    wx.previewImage({
      current: t.target.dataset.bigimg,
      urls: a.data.imgUrlArr
    });
  },
  scrolltoupper: function() {
    var t = this;
    a.default.scrolltoupper(t);
  },
  onLoad: function(t) {
    console.log(t);
    var e = this;
    if (wx.createSelectorQuery) {
      var n = wx.createSelectorQuery();
      n.select("#mjltest").boundingClientRect(), n.exec(function(t) {
        console.log(t), e.setData({
          text_height: t[0].height
        }), wx.getSystemInfo({
          success: function(t) {
            e.setData({
              scroll_height: t.windowHeight - e.data.text_height
            });
          }
        });
      });
    } else e.setData({
      serviceFlag: !0
    }), wx.getSystemInfo({
      success: function(t) {
        e.setData({
          scroll_height: 486
        });
      }
    });
    e.setData({
      token: wx.getStorageSync("logs"),
      doctorId: t.doctorId,
      patientId: t.patientId,
      sessionId: t.sessionId,
      signType: t.signType
    }), a.default.getFirstMsg(e), console.log(e.data.signType), wx.request({
      url: o.globalData.im + "chat/iplist",
      data: {
        token: e.data.token,
        to: e.data.patientId,
        medType: e.data.signType
      },
      success: function(t) {
        console.log(t), 200 == t.data.code && (e.setData({
          chatUrl: t.data.data.chatUrl
        }), e.socket(e));
      }
    }), wx.createInnerAudioContext && (s.onPlay(function() {
      console.log("开始播放"), e.data.oppositeList[e.data.playIndex] && (e.data.oppositeList[e.data.playIndex].playFlag = !0,
        e.setData({
          oppositeList: e.data.oppositeList
        }));
    }), s.onError(function(t) {
      console.log(t.errMsg), console.log(t.errCode);
    }), s.onStop(function() {
      console.log("停止播放");
    }), s.onEnded(function() {
      console.log("播放完毕了"), e.data.oppositeList[e.data.playIndex] && (e.data.oppositeList[e.data.playIndex].playFlag = !1,
        e.setData({
          oppositeList: e.data.oppositeList
        }));
    }));
  },
  socket: function(a) {
    wx.connectSocket({
      url: a.data.chatUrl,
      header: {
        "content-type": "application/json"
      },
      method: "GET",
      success: function(t) {
        console.log(t);
      }
    }), wx.onSocketOpen(function(t) {
      console.log("WebSocket连接已打开！");
      var o = JSON.stringify({
        version: 1,
        operation: 1,
        seqId: new Date().getTime(),
        data: {
          token: a.data.token,
          to: a.data.doctorId
        }
      });
      wx.sendSocketMessage({
        data: o,
        success: function(t) {
          console.log(t);
        }
      });
    }), wx.onSocketMessage(function(s) {
      var n = JSON.parse(s.data);
      switch (console.log(n), n.operation) {
        case 2:
          200 == n.data.code && (a.setData({
            userId: n.data.userId
          }), a.pullMsg(0, 10, a)), a.heartbeat(), t = setInterval(a.heartbeat, 5e4);
          break;

        case 4:
          console.log("心跳ok");
          break;

        case 6:
          console.log(n), a.data.msgIdArr.push(n.data.msgId), a.data.seqIdArr.push(parseInt(n.data.seqId));
          break;

        case 8:
          console.log(n);
          break;

        case 9:
          var i = a.data.oppositeList,
            d = a.data.imgUrlArr,
            l = a.data.imgUrl;
          console.log(a.data.oppositeList), console.log(n.data), n.data.ctime = o.toDate(n.data.ctime),
            "SINGLE_IMAGE" == n.data.msgType ? (l = n.data.msg.split("|"), n.data.msg = l[0],
              d.push(l[1]), n.data.bigImg = l[1], i.push(n.data), a.setData({
                oppositeList: i,
                imgUrlArr: d,
                scrollTop: a.data.scrollTop + 1e3
              })) : (i.push(n.data), a.setData({
              oppositeList: i,
              scrollTop: a.data.scrollTop + 1e3
            }));
          var c = {
              version: 1,
              operation: 7,
              seqId: new Date().getTime(),
              data: {
                to: n.data.from,
                msgId: n.data.msgId
              }
            },
            r = JSON.stringify(c);
          wx.sendSocketMessage({
            data: r,
            success: function(t) {}
          });
          break;

        case 10:
          console.log(n);
          break;

        case 14:
          console.log(n), n.data.sessionid == a.data.sessionId && a.setData({
            disableFlag: !0
          }), console.log("对方结束了本次咨询：sessionId:" + n.data.sessionid + ", from:" + n.data.from + ",msg:" + n.data.msg);
          break;

        case 16:
          console.log(n);
          for (var g = n.data.msgId, i = a.data.oppositeList, p = 0; p < i.length; p++)
            if (i[p].msgId == g) {
              i.splice(p, 1), a.setData({
                oppositeList: i
              });
              break;
            }
          console.log("对方撤回了消息，sessionId:" + n.data.sessionId + ", from:" + n.data.from + ",msgId:" + n.data.msgId);
          break;

        case 12:
          var u = [];
          if (null != n.data.list && (u = n.data.list, console.log(n.data.list), u.forEach(function(t, o) {
              var s = a.data.oppositeList,
                n = a.data.imgUrlArr,
                i = a.data.imgUrl;
              console.log(e), 0 == o && 0 == e && (t.parting = 1), e++, t.msg = t.message, t.from = t.msgFrom,
                "SINGLE_IMAGE" == t.msgType ? (i = t.msg.split("|"), t.msg = i[0], n.push(i[1]),
                  t.bigImg = i[1], s.push(t)) : s.push(t), a.setData({
                  oppositeList: s,
                  imgUrlArr: n,
                  scrollTop: a.data.scrollTop + 2e3
                });
            })), null != n.data.list && n.data.list.length > 0) {
            g = n.data.list[n.data.list.length - 1].msgId;
            console.log("msgId:" + g), a.pullMsg(g, 10, a);
          } else wx.closeSocket({
            success: function(t) {
              console.log(t);
            }
          }), wx.onSocketClose(function(a) {
            console.log("WebSocket 已关闭！"), console.log(a), clearInterval(t);
          });
      }
    });
  },
  heartbeat: function() {
    var t = {
        version: 1,
        operation: 3,
        seqId: new Date().getTime(),
        data: {}
      },
      a = JSON.stringify(t);
    wx.sendSocketMessage({
      data: a,
      success: function(t) {}
    });
  },
  pullMsg: function(t, a, o) {
    console.log(o.data.sessionId);
    var e = {
        version: 1,
        operation: 11,
        seqId: new Date().getTime(),
        data: {
          userId: o.data.userId,
          offset: t,
          limit: a,
          sessionId: o.data.sessionId,
          medType: o.data.signType
        }
      },
      s = JSON.stringify(e);
    wx.sendSocketMessage({
      data: s,
      success: function(t) {}
    });
  },
  onReady: function() {
    var bgc = wx.getStorageSync('color')
    wx.setNavigationBarColor({
      frontColor: "#ffffff",
      backgroundColor: bgc,
    })
  },
  onShow: function() {
    e = 0;
  },
  onHide: function() {},
  onUnload: function() {
    wx.closeSocket({
      success: function(t) {
        console.log(t);
      }
    }), wx.onSocketClose(function(a) {
      console.log("WebSocket 已关闭！"), console.log(a), clearInterval(t);
    });
  },
  toDoctor: function() {
    var t = this;
    1 == this.data.contactList.doctorBaseInfoVO.jobType ? wx.navigateTo({
      url: "/hyb_yl/userCommunicate/pages/myDoctorIntro/myDoctorIntro?&doctorId=" + t.data.doctorId + "&jobType=" + t.data.contactList.doctorBaseInfoVO.jobType + "&lookOverBoo=1"
    }) : 2 == this.data.contactList.doctorBaseInfoVO.jobType && wx.navigateTo({
        url: "/hyb_yl/userCommunicate/pages/nurseInfro/nurseInfro?&doctorId=" + t.data.doctorId + "&jobType=" + t.data.contactList.doctorBaseInfoVO.jobType + "&lookOverBoo=1"
    });
  }
});

var s = wx.createInnerAudioContext ? wx.createInnerAudioContext() : null;